
%union {
    int ival;
    char* istr;
    void* iptr;
}

%token INT 
%token ADD MINUS MUL DIV
%token LEFT_PAREN RIGHT_PAREN
%type<ival> INT

%type<iptr> expr
%type<iptr> term
%type<iptr> factor

%{
    #include <stdio.h>
    #include "tokens.h"

    int exp_lex(void);
    void exp_error(const char *a){
        printf("error %s called\n",a);
    }
%}

%%
statement:{}
    | expr statement{
        set_root($1);
        mid_tra((inode*)$1);
        printf("\n");
    };

expr: term ADD expr{
        $$ = create_node(-1,$1,$3,0);
    };
    | term MINUS expr{
        $$ = create_node(-1,$1,$3,1);
    };
    | term {
        $$ = $1;
    };

term: factor DIV factor{
        $$ = create_node(-1,$1,$3,3);
    }
    | factor MUL factor{
        $$ = create_node(-1,$1,$1,2);
    }
    | factor {
        $$ = $1; 
    }
    ;

factor: INT {
        $$ = create_node($1,NULL,NULL,-1);
    };
    | LEFT_PAREN expr RIGHT_PAREN {
        $$ = $2;
    };